package com.yanxiaohui.config.order;


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Description TODO
 * @Author： yanxh<br>
 * @Date 2019/11/14 14:48<br>
 * @Version 1.0<br>
 */
@Configuration
@MapperScan(basePackages = "com.yanxiaohui.order", sqlSessionTemplateRef = "orderSqlSessionTemplate")
public class OrderMybatisConfig {
    @Bean(name = "orderDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.order")
    public DataSource orderDataSource(){
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "orderSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("orderDataSource") DataSource dataSource) throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();

    }

    @Bean(name = "orderSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("orderSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "orderTransactionManager")
    public DataSourceTransactionManager sqlSessionTemplate(@Qualifier("orderDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
